﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;

public class GameManager : MonoBehaviour
{
    // 控制游戏进程
    // 初始化游戏信息
    // 记录游戏中一些数值
    // 持有敌人部分对象作用
    // 控制游戏显示状态
    // 控制游戏UI
    [Space]
    public EnemyManager enemyManager;
    public PlayerManager playerManager;

    [Header ("Setup")]
    public Enemy[] enemyList;
    public Weapons[] weaponsList;

    [Header ("Player Attributes")]
    [Range(10, 99)]
    public int playerAge = 10;
    [Range(1, 200)]
    public int playerLevel = 1;
    [Space]
    public int playerExperience;
    public int playerMoney;
    [Space]
    public float playerHealth;
    public float playerMana;
    [Space]
    public int strength;
    public int agility;
    public int intelligence;
    public int willpower;
    public int luck;

    [Header("Unity Setup")]
    public TextMeshProUGUI playerNameUI;
    public TextMeshProUGUI playerRacesUI;
    public TextMeshProUGUI playerAgeUI;
    public TextMeshProUGUI playerLevelUI;
    public Image playerHealthBar;
    public Image playerManaBar;
    public Image playerExperienceBar;
    [Space]
    public TextMeshProUGUI baseStrengthUI;
    public TextMeshProUGUI baseAgilityUI;
    public TextMeshProUGUI baseIntelligenceUI;
    public TextMeshProUGUI baseWillpowerUI;
    public TextMeshProUGUI baseLuckUI;
    [Space]
    public TextMeshProUGUI strengthUI;
    public TextMeshProUGUI agilityUI;
    public TextMeshProUGUI intelligenceUI;
    public TextMeshProUGUI willpowerUI;
    public TextMeshProUGUI luckUI;
    [Space]
    public TextMeshProUGUI attackUI;
    [Space]
    public TextMeshProUGUI monsterNameUI;
    public Image monsterHealthBar;

    // Start is called before the first frame update
    private void Start()
    {
        playerNameUI.text = playerManager.playerName + " " + playerManager.playerTitle;
        playerRacesUI.text = playerManager.playerRaces.ToString();
        playerHealth = playerManager.playerInitialHealth;   // 初始化生命值
        GameInitialize();
        PlayerInitialize();
        InvokeRepeating("Turn", 0f, 1f);
    }

    private void Update()
    {
        // 每一帧都要判断的东西
        // UI更新
        RefreshUI();
        playerManager.CalculateStats();
    }

    void GameInitialize()
    {
        enemyList = Resources.LoadAll<Enemy>("Enemies");
        weaponsList = Resources.LoadAll<Weapons>("Equipments");
    }

    void PlayerInitialize()
    {
        // 初始化角色属性数值
        switch(playerManager.playerRaces)
        {
            case Races.Developer:
                playerManager.playerStrength = 5;
                playerManager.playerAgility = 5;
                playerManager.playerIntelligence = 5;
                playerManager.playerWillpower = 5;
                playerManager.playerLuck = 5;
                break;
            case Races.Human:
                break;
            case Races.Elf:
                break;
            case Races.Giant:
                break;
            case Races.Undead:
                break;
            case Races.Dwarf:
                break;
        }
    }

    void RefreshUI() //UI刷新
    {
        // 角色面板
        playerAgeUI.text = playerAge.ToString();
        playerLevelUI.text = playerLevel.ToString();
        playerHealthBar.fillAmount = playerHealth / playerManager.playerInitialHealth;
        playerManaBar.fillAmount = playerMana / playerManager.playerInitialMana;
        baseStrengthUI.text = "(" + playerManager.playerStrength.ToString() + ")";
        baseAgilityUI.text = "(" + playerManager.playerAgility.ToString() + ")";
        baseIntelligenceUI.text = "(" + playerManager.playerIntelligence.ToString() + ")";
        baseWillpowerUI.text = "(" + playerManager.playerWillpower.ToString() + ")";
        baseLuckUI.text = "(" + playerManager.playerLuck.ToString() + ")";
        attackUI.text = playerManager.playerAttackMin.ToString("F0") + " ~ " + playerManager.playerAttackMax.ToString("F0");

        // 怪物面板
        monsterNameUI.text = enemyManager.enemy.enemyName;
        monsterHealthBar.fillAmount = enemyManager.enemyCurrentHealth / enemyManager.enemy.enemyHealth;
    }

    // 主要战斗功能
    void Turn()  // 一个回合
    {
        if (enemyManager.enemySpawned)
        {
            PlayerAttack();
            // 如果敌人没死亡，敌人攻击
            if (!CheckEnemyDown())
            {
                EnemyAttack();
            }
            // 敌人攻击之后检查角色死亡
            CheckPlayerDown();
        }
        else
        {
            SpawnEnemy();
        }
    }

    void PlayerAttack() // 玩家攻击
    {
        float dmg = Random.Range(playerManager.playerAttackMin, playerManager.playerAttackMax);
        // 计算敌人护甲（伤害直接扣除护甲）
        Debug.Log("Attacked! " + "Damage: " + dmg);
        // Console 输出 "玩家名 对 怪物名 造成 ? 伤害"
        enemyManager.enemyCurrentHealth -= dmg;
    }

    void EnemyAttack() // 敌人攻击
    {
        int dmg = Random.Range(enemyManager.enemy.enemyAttackDown, enemyManager.enemy.enemyAttackUp);
        // 计算玩家护甲（伤害直接扣除玩家）
        Debug.Log("Enemy Attacked! " + "Damage: " + dmg);
        // Console 输出 "敌人名 对 玩家名 造成 ? 伤害"
       playerHealth -= dmg;
    }

    bool CheckEnemyDown() // 判断敌人死亡
    {
        // 判断敌人死亡
        if (enemyManager.enemyCurrentHealth <= 0)
        {
            Debug.Log("Enemy Down.");
            // Console 输出 怪物被击败了
            // 掉落物品
            // 掉落金币
            playerMoney += enemyManager.enemy.value;
            Debug.Log("Money Gained: " + enemyManager.enemy.value);
            // 增加经验
            playerExperience += enemyManager.enemy.experienceGain;
            Debug.Log("Experience Gained: " + enemyManager.enemy.experienceGain);
            enemyManager.enemy = null;
            enemyManager.enemySpawned = false;
            return true;
        }
        else
        {
            return false;
        }
    }

    void SpawnEnemy() // 生成敌人
    {
        if (!enemyManager.enemySpawned)
        {
            Debug.Log("EnemySpawned");
            int x;
            x = Random.Range(0, enemyList.Length - 1);
            enemyManager.enemy = enemyList[x]; // 生成敌人
            enemyManager.enemyCurrentHealth = enemyManager.enemy.enemyHealth; // 初始化血量
            enemyManager.enemySpawned = true;
        }
    }
    
    void CheckPlayerDown() // 判断玩家死亡
    {
        // 角色死亡之后
        if (playerHealth <= 0)
        {
            Debug.Log("Player Down!");
            enemyManager.enemy = null;
            enemyManager.enemySpawned = false;
            playerHealth = playerManager.playerInitialHealth;
        }
    }

    void Looting() // 怪物掉落
    {
        /* 判断怪物是否掉落
         * 掉落物品总类
         * 掉落物品，并确定物品品质
         */
    }
}